package wuke.show;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.PhoneLookup;
import android.provider.ContactsContract.CommonDataKinds.Photo;

import wuke.model.Contact;

public class DBUtil {

	private Context mContext;

	private List<Contact> mContacts = new ArrayList<Contact>();

	public DBUtil(Context mContext) {

		this.mContext = mContext;
	}

	// get contacts : include name , phone numbers , photoURI
	public List<Contact> queryContacts() {

		Cursor cursor = mContext.getContentResolver().query(
				ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

		while (cursor.moveToNext()) {

			Contact contact = new Contact();

			// query contact id
			String contactId = queryContact_ID(cursor);
			contact.setContactId(contactId);
			System.out.println("contantId----->" + contactId);

			// query contact name
			String name = queryContactName(cursor);
			contact.setName(name);

			// query contact numbers
			queryPhoneNumber(cursor, contact);

		}

		cursor.close();

		// query contacts photoURI
		queryPhotoURI();

		return mContacts;
	}

	private String queryContact_ID(Cursor cursor) {

		String contactId = cursor.getString(cursor
				.getColumnIndex(ContactsContract.Contacts._ID));

		return contactId;
	}

	private String queryContactName(Cursor cursor) {

		int nameFieldIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
		String name = cursor.getString(nameFieldIndex);
		System.out.println("name-------->" + name);

		return name;

	}

	private void queryPhoneNumber(Cursor cursor, Contact contact) {

		String hasPhone = cursor.getString(cursor
				.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

		System.out.println("hasPhone------>" + hasPhone);

		if (Integer.parseInt(hasPhone) == 1) {
			List<String> phoneNumbs = contact.getPhoneNumbers();
			Cursor phones = mContext.getContentResolver().query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
					null,
					ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
							+ contact.getContactId(), null, null);
			System.out.println(phones.getCount());
			while (phones.moveToNext()) {
				String phoneNumber = phones
						.getString(phones
								.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				System.out.println("phoneNuber------>" + phoneNumber);
				phoneNumbs.add(phoneNumber);
			}

			phones.close();

		}

		mContacts.add(contact);

	}

	private void queryPhotoURI() {

		for (int i = 0; i < mContacts.size(); i++) {

			String sql = Data.CONTACT_ID + "="
					+ mContacts.get(i).getContactId() + " AND " + Data.MIMETYPE
					+ "='" + Photo.CONTENT_ITEM_TYPE + "'";
			System.out.println("sql-------->" + sql);

			Cursor cur = mContext.getContentResolver().query(Data.CONTENT_URI,
					null, sql, null, null);

			if (cur != null) {
				if (cur.moveToFirst()) {

					Uri person = ContentUris.withAppendedId(
							ContactsContract.Contacts.CONTENT_URI,
							Long.parseLong(mContacts.get(i).getContactId()));
					String strUri = Uri.withAppendedPath(person,
							ContactsContract.Contacts.Photo.CONTENT_DIRECTORY)
							.toString();

					mContacts.get(i).setPhotoURI(strUri);

					System.out.println("photo URI----->"
							+ mContacts.get(i).getPhotoURI());

				}
			}

			cur.close();

		}

	}

}
